package org.se.scut.pinterest.mina.codec.filte.coder.decoder;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.AttributeKey;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;
import org.apache.mina.filter.codec.demux.MessageDecoderResult;
import org.se.scut.pinterest.mina.codec.coder.cxt.BaseContext;
import org.se.scut.pinterest.mina.codec.coder.cxt.PicturesGetContext;
import org.se.scut.pinterest.mina.codec.msg.PicturesGetMessage;

public class PicturesGetDecoder extends BaseMessageDecoder {

	public PicturesGetDecoder() {
		super(new AttributeKey(PicturesGetDecoder.class, "context"), "mcappbtup");
	}

	@Override
	public MessageDecoderResult decode(IoSession session, IoBuffer in,
			ProtocolDecoderOutput out) throws Exception {
		PicturesGetContext context = (PicturesGetContext)getContext(session);
		IoBuffer buffer = context.getInnerBuffer();
		int matchCount = context.getMatchCount();
		int lineNumber = context.getLineNumber();
		String symbol = context.getSymbol();
		String userID = context.getUserID();
		String ablumID = context.getAblumID();
		String pictureTypeID = context.getPictureTypeID();
		String from = context.getFrom();
		String count = context.getCount();
		byte b;
		while (in.hasRemaining()) {
			b = in.get();
			matchCount++;
			buffer.put(b);
			if (b == 10) {
				if (lineNumber == 0) {
					buffer.flip();
					symbol = buffer.getString(matchCount, DECODER);
					symbol = symbol.substring(0, symbol.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setSymbol(symbol);
				}
				if (lineNumber == 1) {
					buffer.flip();
					userID = buffer.getString(matchCount, DECODER);
					userID = userID.substring(0, userID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setUserID(userID);
				}
				if (lineNumber == 2) {
					buffer.flip();
					ablumID = buffer.getString(matchCount, DECODER);
					ablumID = ablumID.substring(0, ablumID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setAblumID(ablumID);
				}
				if (lineNumber == 3) {
					buffer.flip();
					pictureTypeID = buffer.getString(matchCount, DECODER);
					pictureTypeID = pictureTypeID.substring(0, pictureTypeID.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setPictureTypeID(pictureTypeID);
				}
				if (lineNumber == 4) {
					buffer.flip();
					from = buffer.getString(matchCount, DECODER);
					from = from.substring(0, from.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setFrom(from);
				}
				if (lineNumber == 5) {
					buffer.flip();
					count = buffer.getString(matchCount, DECODER);
					count = count.substring(0, count.length() - 1);
					matchCount = 0;
					buffer.clear();
					context.setCount(count);
					break;
				}
				lineNumber++;
			}
			context.setMatchCount(matchCount);
			context.setLineNumber(lineNumber);
		}
		if (context.getLineNumber() == 5) {
			PicturesGetMessage msg = new PicturesGetMessage(context.getSymbol());
			msg.setUserID(Integer.parseInt(context.getUserID()));
			msg.setAblumID(Integer.parseInt(context.getAblumID()));
			msg.setAblumID(Integer.parseInt(context.getAblumID()));
			msg.setPictureTypeID(Integer.parseInt(context.getPictureTypeID()));
			msg.setFrom(Integer.parseInt(context.getFrom()));
			msg.setCount(Integer.parseInt(context.getCount()));
			context.resetData();
			out.write(msg);
			return MessageDecoderResult.OK;
		}else {
			return MessageDecoderResult.NEED_DATA;
		}
	}

	
	@Override
	public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
			throws Exception {

	}

	@Override
	public BaseContext getContext(IoSession session) {
		PicturesGetContext context = (PicturesGetContext)session.getAttribute(CONTEXT_KEY);
		if (context == null) {
			context = new  PicturesGetContext();
			session.setAttribute(CONTEXT_KEY, context);
		}
		return context;
	}

}
